/**
 * Copyright (C) 2010 Neofonie GmbH
 *
 * This programm is free software; you can redistribute it and/or modify
 * it under the terms of the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.dicodeproject.analysis.hashtags;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;
import org.apache.hadoop.util.ToolRunner;
import org.apache.mahout.common.AbstractJob;
import org.apache.mahout.common.StringTuple;
import org.apache.mahout.common.commandline.DefaultOptionCreator;

import java.util.Map;

/**
 * Reads text from a configurable HBase table and column, extracts all hashtags and writes day/hashtag pairs and counts to HDFS.
 */
public class HashtagsTimelineDriver extends AbstractJob {
  
  private HashtagsTimelineDriver() {
    // don't instantiate drivers
  }

  public static void main(String args[]) throws Exception {
    ToolRunner.run(new HashtagsTimelineDriver(), args);
  }

  @Override
  public int run(String[] args) throws Exception {
    addOutputOption();
    addOption(DefaultOptionCreator.numReducersOption().create());  
    addOption("table", "t", "The hbase table holding our data.", "twittertracker");
    addOption("family", "f", "The column family holding our data.", "textFamily");
    addOption("column", "c", "The column holding our data.", "text");
    addOption("familyT", "y", "The column family holding our timestamps.", "metaFamily");
    addOption("columnT", "z", "The column holding our timestamps.", "creationDate");

    Map<String, String> argMap = parseArguments(args);
    if (argMap == null) {
      return -1;
    }
    String table = argMap.get("--table");
    String family = argMap.get("--family");
    String column = argMap.get("--column");
    String timeFamily = argMap.get("--familyT");
    String timeColumn = argMap.get("--columnT");
    
    Path output = getOutputPath();

    Configuration conf = HBaseConfiguration.create();
    Job job = new Job(conf);
    job.setJarByClass(HashtagsTimelineDriver.class);

    Scan scan = new Scan();
    scan.addColumn(Bytes.toBytes(family), Bytes.toBytes(column));
    scan.addColumn(Bytes.toBytes(timeFamily), Bytes.toBytes(timeColumn));
    scan.setMaxVersions(1);
    TableMapReduceUtil.initTableMapperJob(table, scan, HashtagsTimelineMapper.class, Text.class,
        StringTuple.class, job);

    job.setJobName("HBaseDocumentProcessor::HashtagsTimelineCounter");
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    
	job.setReducerClass(HashtagsReducer.class);

	job.setMapOutputKeyClass(Text.class);
	job.setMapOutputValueClass(IntWritable.class);

	FileOutputFormat.setOutputPath(job, output);
	job.setOutputFormatClass(SequenceFileOutputFormat.class);
    job.setNumReduceTasks(10);
    job.waitForCompletion(true);
    return 0;
  }
}